home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / LinAlg 3.1 / LinAlg / vhjmin.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-20  |  5.3 KB  |  199 lines  |  [TEXT/ALFA]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *           Verify the Hook-Jeevse minimization
  6.  *
  7.  ************************************************************************
  8.  */
  9.  
  10. #include "LinAlg.h"
  11. #include "math_num.h"
  12. #include "builtin.h"
  13. #include <iostream.h>
  14.  
  15.  
  16. static int iter_count;
  17.  
  18. /*
  19.  *-----------------------------------------------------------------------
  20.  *             Simplified vector print
  21.  */
  22.  
  23. static void pr_vector(Vector& v)
  24. {
  25.   register int i;
  26.   for(i=v.q_lwb(); i<=v.q_upb(); i++)
  27.      printf("%9.3g  ",v(i));
  28. }
  29.  
  30.  
  31. /*
  32.  *------------------------------------------------------------------------
  33.  *              Rosenbroke function
  34.  */
  35.  
  36. static double f1(const Vector& v)
  37. {
  38.   register double x1 = v(1);
  39.   register double x2 = v(2);
  40.   iter_count++;
  41.   return 100*sqr(x2 - x1*x1) + sqr(1 - x1);
  42. }
  43.  
  44. static void test1()
  45. {
  46.   const int n = 2;
  47.   Vector b0(1,n,-1.2,1.,"END");            // Initial guess
  48.   Vector b = b0;                // Min location found
  49.   Vector bm(1,n,1.0,1.0,"END");            // Exact min location
  50.   Vector h(1,n,10.,10.,"END");            // Initial step
  51.  
  52.   iter_count = 0;
  53.   cout << "\n\n\tRosenbroke function\n";
  54.   cout << "\n\n\tf = 100*(x2-x1^2)^2 + (1-x1)^2\n\n";
  55.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  56.   cout << "\nFunction value at it  f0 = " << f1(b0);
  57.   cout << "\nInitial steps         h0 = "; pr_vector(h);
  58.   cout << "\n";
  59.   cout << "\nMinimum f value found f  = " << hjmin(b,h,f1);
  60.   cout << "\n                  at  b  = "; pr_vector(b);
  61.   cout << "\nFinal steps           h = "; pr_vector(h);
  62.   cout << "\nExact min location    bm = "; pr_vector(bm);
  63.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  64.  
  65. }
  66.  
  67.  
  68.  
  69. /*
  70.  *------------------------------------------------------------------------
  71.  *                 Bocks function
  72.  */
  73.  
  74. static double f2(const Vector& v)
  75. {
  76.   register double x1 = v(1);
  77.   register double x2 = v(2);
  78.   iter_count++;
  79.   return sqr( exp(-1./10) - exp(-x1/10) + exp(-10./10) - exp(-x2/10) );
  80. }
  81.  
  82. static void test2()
  83. {
  84.   const int n = 2;
  85.   Vector b0(1,n,0.,0.,"END");            // Initial guess
  86.   Vector b =b0;                    // Min location found
  87.   Vector bm(1,n,10.0,1.0,"END");        // Exact min location
  88.   double h0=10;                    // Initial step
  89.  
  90.   iter_count = 0;
  91.   cout << "\n\n\tBocks function\n";
  92.   cout << "\n\n\t"
  93.       "f = [ exp(-1/10) - exp(-x1/10) + exp(-10/10) -exp(-x2/10) ]^2\n\n";
  94.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  95.   cout << "\nFunction value at it  f0 = " << f2(b0);
  96.   cout << "\nInitial steps         h0 = " << h0;
  97.   cout << "\n";
  98.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f2);
  99.   cout << "\n                  at  b  = "; pr_vector(b);
  100.   cout << "\nExact min location    bm = "; pr_vector(bm);
  101.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  102.  
  103. }
  104.  
  105.  
  106.  
  107. /*
  108.  *------------------------------------------------------------------------
  109.  *            Mile & Cuntrell function
  110.  */
  111.  
  112. static double f3(const Vector& v)
  113. {
  114.   register double x1 = v(1);
  115.   register double x2 = v(2);
  116.   register double x3 = v(3);
  117.   register double x4 = v(4);
  118.   iter_count++;
  119.   return pow( exp(x1)-x2, 4L) + 100*pow(x2-x3,6L) + pow(atan(x3-x4),4L) +
  120.      pow(x1,8L);
  121. }
  122.  
  123. static void test3()
  124. {
  125.   const int n = 4;
  126.   Vector b0(1,n,1.,2.,2.,2.,"END");        // Initial guess
  127.   Vector b = b0;                    // Min location found
  128.   Vector bm(1,n,0.,1.,1.,1.,"END");        // Exact min location
  129.   double h0 = 10;                // Initial step
  130.  
  131.   iter_count = 0;
  132.   cout << "\n\n\tMile & Cuntrell function\n";
  133.   cout << "\n\n\t"
  134.       "f = [ exp(x1)-x2 ]^4 +100(x2-x3)^6 + atan(x3-x4)^4 + x1^8\n\n";
  135.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  136.   cout << "\nFunction value at it  f0 = " << f3(b0);
  137.   cout << "\nInitial steps         h0 = " << h0;
  138.   cout << "\n";
  139.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f3);
  140.   cout << "\n                  at  b  = "; pr_vector(b);
  141.   cout << "\nExact min location    bm = "; pr_vector(bm);
  142.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  143.  
  144. }
  145.  
  146. /*
  147.  *------------------------------------------------------------------------
  148.  *              Powell function
  149.  */
  150.  
  151. static double f4(const Vector& v)
  152. {
  153.   register double x1 = v(1);
  154.   register double x2 = v(2);
  155.   register double x3 = v(3);
  156.   register double x4 = v(4);
  157.   iter_count++;
  158.   return sqr(x1+10*x2) + 5*sqr(x3-x4) + pow(x2-2*x3,4L) + 10*pow(x1-x4,4L);
  159. }
  160.  
  161. static void test4()
  162. {
  163.   const int n = 4;
  164.   Vector b0(1,n,3.,-1.,0.,1.,"END");        // Initial guess
  165.   Vector b = b0;                // Min location found
  166.   Vector bm(1,n,0.,0.,0.,0.,"END");        // Exact min location
  167.   Vector h0(1,n,10.,10.,10.,10.,"END");        // Initial step
  168.  
  169.   iter_count = 0;
  170.   cout << "\n\n\tPowell function\n";
  171.   cout << "\n\n\t" 
  172.       "f = (x1+10*x2)^2 + 5(x3-x4)^2 + (x2-2x3)^4 + 10(x1-x4)^4\n\n";
  173.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  174.   cout << "\nFunction value at it  f0 = " << f4(b0);
  175.   cout << "\nInitial steps         h0 = "; pr_vector(h0);
  176.   cout << "\n";
  177.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f4);
  178.   cout << "\n                  at  b  = "; pr_vector(b);
  179.   cout << "\nExact min location    bm = "; pr_vector(bm);
  180.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  181.  
  182. }
  183.  
  184.  
  185. /*
  186.  *------------------------------------------------------------------------
  187.  *                Root module
  188.  */
  189.  
  190. main()
  191. {
  192.   cout << "\n\n\n\t\tVerify HJMIN multidimensional minimizer\n";
  193.   test1();
  194.   test2();
  195.   test3();
  196.   test4();
  197. }
  198.  
  199.